package expiring;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Test {
	public static void main(String[] args) {
		HashMap<String,String> ageDomain = new HashMap<String,String>();
		HashMap<String,String> alexaDomain = new HashMap<String,String>();
		HashMap<String,String> nameDomain = new HashMap<String,String>();
		HashMap<String,String> priceDomain = new HashMap<String,String>();
		
		try {
			String content = "";
			URL dataURL = new URL("https://manage.name.com/expired_domains.php?first=1&tld=com&length=10&hours24=on&noNumber=on&noDash=on&placement=contains&count=20&searchTerm=");
			HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
			connection.setReadTimeout(500000);
			connection.setConnectTimeout(1000000);
			connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cache-Control", "max-age=0");
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF8")));
			while(1==1)
			{
				String str = reader.readLine();
				if(str==null) break;
				content+=str;
				
			}
			
			Document doc = Jsoup.parse(content);
			
			Elements tr = doc.select(".page").select(".largetable").get(2).select("tr");
			
			String strs = "";
			if(tr.size() > 0)
			{
				for (int i = 0; i < tr.size(); i++) {
					if(i > 2 && i < (tr.size()-2))
					{
						String name = tr.get(i).select("td").get(1).text().trim();
						new AgeDomain(name,ageDomain);
						
						nameDomain.put(name, name);

						priceDomain.put(name, tr.get(i).select("td").get(3).text());

						new Alexa(name,alexaDomain);
					}
				}
			}
			try {
				Thread.sleep(5000);
				NumberFormat nf = NumberFormat.getInstance();
				String alexa = "-";
				for (String key : nameDomain.keySet()) {
					 if(!alexaDomain.get(key).equals("-"))
					 {
						 alexa = nf.format(Integer.parseInt(alexaDomain.get(key).toString()));
					 }
					 strs += "<tr><td>"+nameDomain.get(key)+"</td>";
					 strs += "<td class=\"center\">"+priceDomain.get(key)+"</td>";
					 strs += "<td class=\"center\"><img src=\"http://prapi.net/pr.jsp?url=http://"+nameDomain.get(key)+"\"/></td>";
					 strs += "<td class=\"center\">"+alexa+"</td>";
					 strs += "<td class=\"right\">"+ageDomain.get(key)+"</td></tr>";
				}
				System.out.println(strs);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
